import 'package:flutter/material.dart';
import 'package:flutterclue/mvp/page/LoginPage.dart';
import 'package:flutterclue/utils/JpushUtil.dart';
import 'package:flutterclue/utils/SharePreferensUtil.dart';
import 'package:flutterclue/mvp/presenter/DrawerPagePresenter.dart';
import 'package:flutterclue/utils/ShowSnackBarUtil.dart';
import 'package:flutterclue/utils/ToastUtilForAndroid.dart';
import 'package:flutterclue/view/LoadingDialogView.dart';

class DrawerPage extends StatefulWidget {
  @override
  _DrawerPageState createState() => new _DrawerPageState();
}

class _DrawerPageState extends State<DrawerPage> implements LoginViewOut {
  String _userName = "";
  String _userType = "";

  LoginOutPresenter _loginPresenter = new LoginOutPresenter();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getUserInfo();
    _loginPresenter.attachView(this);
  }

  _getUserInfo() async {
    _userName = await SharePreferencesUtil.getString(SharePreferencesUtil.Name);
    _userType =
        await SharePreferencesUtil.getString(SharePreferencesUtil.RoleName);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      margin: new EdgeInsets.only(right: 100),
      child: new Scaffold(
        body: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            new Container(
              decoration: new BoxDecoration(color: new Color(0xff0188fb)),
              child: new Padding(
                  child: new Row(
                    children: <Widget>[
                      new Image.asset(
                        "images/ic_head_default.png",
                        height: 64,
                        width: 64,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: new Column(children: <Widget>[
                          new Text(
                            _userName,
                            style: new TextStyle(
                                color: Colors.white, fontSize: 18.0),
                          ),
                          new Padding(
                              padding: new EdgeInsets.only(top: 5.0),
                              child: new Container(
                                padding: new EdgeInsets.all(3.0),
                                decoration: new BoxDecoration(
                                    color: new Color(0xff7DC2FF),
                                    borderRadius: new BorderRadius.all(
                                        new Radius.circular(8.0))),
                                child: new Text(
                                  _userType,
                                  style: new TextStyle(
                                      color: Colors.white, fontSize: 11.0),
                                ),
                              ))
                        ]),
                      )
                    ],
                  ),
                  padding: new EdgeInsets.only(top: 80, left: 16, bottom: 50)),
            ),
            new Padding(
              padding: const EdgeInsets.only(top: 20),
              child: SizedBox(
                height: 50,
                child: new RaisedButton(
                    color: Colors.white,
                    child: new Row(
                      children: <Widget>[
                        new Icon(Icons.forward),
                        new Padding(
                          padding: new EdgeInsets.only(left: 16),
                          child: new Text(
                            '退出',
                            style: new TextStyle(
                                color: Color(0xff333333), fontSize: 14.0),
                          ),
                        )
                      ],
                    ),
                    onPressed: () {
                      _loginPresenter.loginOut();
                    }),
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  dismissLoadingView() {
    Navigator.pop(context);
  }

  @override
  void onLoginFail(String error) {
    ToastUtil.showShort(error);
  }

  @override
  void onLoginOutSuccess() {
    JpushUtil.removeTags();
    SharePreferencesUtil.saveString("clueIdJ", "");
    SharePreferencesUtil.saveString("messageTypeJ", "");
    _loginPresenter.saveJpush();
    Navigator.of(context).pushAndRemoveUntil(
        new MaterialPageRoute(builder: (context) => new LoginPage()),
        (route) => route == null);
  }

  @override
  showLoadingView() {
    showDialog<Null>(
        context: context, //BuildContext对象
        barrierDismissible: false,
        builder: (BuildContext context) {
          return new LoadingDialog(
            //调用对话框
            text: '正在退出...',
          );
        });
  }
}
